package com.rgst.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.rgst.dao.entity.Busi;
import com.rgst.dao.entity.Enroll;
import com.rgst.dao.entity.Student;
import com.rgst.dao.repository.BusiRepository;
import com.rgst.dto.Result;
import com.rgst.dto.UserDto;
import com.rgst.serivce.EnrollService;
import com.rgst.utils.DateUtil;
import com.rgst.vo.EnrollVo;

/**
 * Created by tancw on 2019/11/24.
 */
@Controller
@RequestMapping("enroll")
@Validated
public class EnrollController extends BaseController {

    @Autowired
    private EnrollService enrollService;

    @Autowired
    private BusiRepository busiRepository;

    @GetMapping("show")
    public ModelAndView show(HttpServletRequest request) {
        UserDto userDto = getSessionUser(request);
        List<Enroll> enrolls = enrollService.query(userDto.getId());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", userDto);
        modelAndView.addObject("enrolls", enrolls);
        modelAndView.setViewName("enroll-list");
        return modelAndView;
    }


    @PostMapping("submit")
    public ModelAndView submit(HttpServletRequest request, @Valid EnrollVo enrollVo) {
        UserDto userDto = getSessionUser(request);
        ModelAndView modelAndView = new ModelAndView();

        Busi busi = busiRepository.findById(enrollVo.getBusiId()).get();
        if (busi == null || !busi.isValid()) {
            modelAndView.setViewName("fail");
            modelAndView.addObject("message", "已不能报名");
        }

        Student stu = new Student();
        stu.setRealname(enrollVo.getName());
        stu.setSex(enrollVo.getSex());
        stu.setIdcard(enrollVo.getIdno());
        stu.setParentId(userDto.getId());
        stu.setNation(enrollVo.getNation());
        stu.setCreateTime(new Date());
        stu.setBirthday(DateUtil.getBirthdayByIdNo(enrollVo.getIdno()));

        Enroll enrollDto = new Enroll();
        enrollDto.setBusiId(enrollVo.getBusiId());
        enrollDto.setDep(busi.getDep());
        enrollDto.setSname(stu.getRealname());
        enrollDto.setUname(userDto.getRealname());
        enrollDto.setGrade(enrollVo.getGrade());
        enrollDto.setClassName(enrollVo.getClassName());
        enrollDto.setDescrible(enrollVo.getDesc());
        enrollDto.setRemark(enrollVo.getRemark());
        enrollDto.setUserId(userDto.getId());
        enrollDto.setState(1);
        enrollDto.setEnrollExtFields(enrollVo.getExtFields());
        Result ret = enrollService.submit(stu, enrollDto);
        if (ret.isSuccess()) {
            modelAndView.setViewName("success");
        } else {
            modelAndView.setViewName("error");
            modelAndView.addObject("message", ret.getMsg());
        }
        return modelAndView;
    }

}
